package com.sciencep.pro.core.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sciencep.pro.common.config.Constant;
import com.sciencep.pro.common.domain.OscaConstant;
import com.sciencep.pro.common.domain.QueryRequest;
import com.sciencep.pro.common.domain.ResponseResult;
import com.sciencep.pro.common.utils.SortUtil;
import com.sciencep.pro.core.dao.ProAssignmentMapper;
import com.sciencep.pro.core.domain.dto.assignment.AssignmentTo;
import com.sciencep.pro.core.domain.dto.project.ProAssignmentTO;
import com.sciencep.pro.core.domain.dto.user.UserDetail;
import com.sciencep.pro.core.domain.entity.ProAssignment;
import com.sciencep.pro.core.domain.entity.ProBaseInfo;
import com.sciencep.pro.core.domain.entity.ProPlanHours;
import com.sciencep.pro.core.domain.entity.User;
import com.sciencep.pro.core.service.IProAssignmentService;
import com.sciencep.pro.core.service.IProBaseInfoService;
import com.sciencep.pro.core.service.IProPlanHoursService;
import com.sciencep.pro.core.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Sciencep
 */
@Service
public class ProAssignmentServiceImpl extends ServiceImpl<ProAssignmentMapper, ProAssignment> implements IProAssignmentService {

    @Autowired
    @Lazy
    IProAssignmentService proAssignmentService;

    @Autowired
    IProPlanHoursService proPlanHoursService;

    @Autowired
    @Lazy
    IProBaseInfoService proBaseInfoService;

    @Autowired
    IUserService userService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveAssign(ProAssignmentTO proAssignment, UserDetail userDetail) {
        if (proAssignment == null) {
            return false;
        }

        if (CollUtil.isNotEmpty(proAssignment.getProAssignmentList())) {
            //批量储存人员分配
            List<ProAssignment> proAssignmentList = new ArrayList<>();
            proAssignment.getProAssignmentList().forEach(e -> {
                ProAssignment assignment = new ProAssignment();
                assignment.setCreateUserId(userDetail.getUserId());
                assignment.setCreateUserName(userDetail.getUserName());
                assignment.setIsDel(Constant.IsDelete.NO);

                assignment.setProId(e.getProId());
                assignment.setNodeId(e.getNodeId());
                assignment.setNodeStart(e.getNodeStart());
                assignment.setNodeEnd(e.getNodeEnd());
                assignment.setUserId(e.getUserId());
                assignment.setProroleId(e.getProroleId());
                assignment.setInputType(e.getInputType());
                assignment.setInputStart(e.getInputStart());
                assignment.setInputEnd(e.getInputEnd());
                assignment.setInputHours(e.getInputHours());

                //reviewId,reviewName默认设置成该项目的负责人
                ProBaseInfo proBaseInfo = proBaseInfoService.getOne(new LambdaQueryWrapper<ProBaseInfo>().eq(ProBaseInfo::getId, e.getProId()));
                assignment.setReviewId(proBaseInfo.getProManager());
                assignment.setReviewName(userService.getOne(new LambdaQueryWrapper<User>().eq(User::getUserId,proBaseInfo.getProManager())).getUserName());

                proAssignmentList.add(assignment);
            });
            proAssignmentService.saveBatch(proAssignmentList);
            //保存项目状态
            ProBaseInfo proBaseInfo = new ProBaseInfo();
            if(CollUtil.isNotEmpty(proAssignmentList)) {
                proBaseInfo.setId(proAssignmentList.get(Constant.DefaultVal.INT).getProId());
            }
            proBaseInfo.setProStatus(Constant.ProStatus.PROCESSING);
            proBaseInfoService.updateById(proBaseInfo);
        }

        if (CollUtil.isNotEmpty(proAssignment.getProPlanHoursList())) {
            //批量储存工事计划
            List<ProPlanHours> proPlanHoursList = new ArrayList<>();
            proAssignment.getProPlanHoursList().forEach(e -> {
                ProPlanHours proPlanHours = new ProPlanHours();
                proPlanHours.setCreateUserId(userDetail.getUserId());
                proPlanHours.setCreateUserName(userDetail.getUserName());
                proPlanHours.setIsDel(Constant.IsDelete.NO);

                proPlanHours.setProId(e.getProId());
                proPlanHours.setNodeId(e.getNodeId());
                proPlanHours.setUserId(e.getUserId());
                proPlanHours.setPlanDate(e.getPlanDate());
                proPlanHours.setInputHours(e.getInputHours());

                proPlanHoursList.add(proPlanHours);
            });
            proPlanHoursService.saveBatch(proPlanHoursList);
        }

        return true;
    }

    @Override
    public boolean updateReadType(ProAssignmentTO proAssignmentTO, UserDetail userDetail){
        ProAssignment proAssignment = new ProAssignment();
        proAssignment.setModifyDate(new Date());
        proAssignment.setModifyUserId(userDetail.getUserId());
        proAssignment.setModifyUserName(userDetail.getUserName());
        proAssignment.setReadType(Constant.ProStatus.TO_AUDIT);
        proAssignmentService.update(proAssignment,new LambdaQueryWrapper<ProAssignment>().eq(ProAssignment::getUserId,userDetail.getUserId()).eq(ProAssignment::getProId,proAssignmentTO.getProId()));
        return true;
    }

    @Override
    public Page<AssignmentTo> getReviewList(QueryRequest queryRequest,AssignmentTo assignmentTo) {
        Page<AssignmentTo> page = new Page<>();
        SortUtil.handlePageSort(queryRequest, page, "id", OscaConstant.ORDER_DESC, false);
        return baseMapper.getReviewList(page,assignmentTo);
    }

    @Override
    public int updateReview(AssignmentTo assignmentTo) {
        ProAssignment proAssignment = new ProAssignment();
        proAssignment.setReviewId(assignmentTo.getReviewId());
        proAssignment.setReviewName(assignmentTo.getReviewName());
        return baseMapper.update(proAssignment,new LambdaQueryWrapper<ProAssignment>().
                eq(ProAssignment::getProId,assignmentTo.getProId()).
                eq(ProAssignment::getUserId,assignmentTo.getUserId()));
    }
}
